package com.qiye.pointsserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qiye.pointsserver.common.PageUtil.PageParam;
import com.qiye.pointsserver.common.PageUtil.PageResult;
import com.qiye.pointsserver.common.result.Result;
import com.qiye.pointsserver.model.dto.AutherDto;
import com.qiye.pointsserver.model.dto.ProductDto;
import com.qiye.pointsserver.model.entity.Auther;
import com.qiye.pointsserver.model.entity.Product;
import com.qiye.pointsserver.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "03.产品积分模块")
@RestController
@RequestMapping("/api/v1/product")
@RequiredArgsConstructor
@Slf4j
public class ProductController {
    
    @Resource
    private ProductService productService;


    @Operation(summary = "添加产品积分")
    @PostMapping("/add")
    public Result add(@RequestBody ProductDto productDto){
        LambdaQueryWrapper<Product> productLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productLambdaQueryWrapper.eq(Product::getName, productDto.getName());
        if (productService.count(productLambdaQueryWrapper) > 0){
            return Result.failed("该产品已存在已存在");
        }
        Product product = new Product();
        product.setName(productDto.getName());
        product.setPoints(productDto.getPoints());
        product.setIsCompany(productDto.getIsCompany());
        product.setTime(productDto.getTime());
        product.setDesc(productDto.getDesc());
        productService.save(product);
        return Result.success("添加成功");
    }

    @Operation(summary = "修改产品积分")
    @PostMapping("/update")
    public Result update(@RequestBody ProductDto productDto){
        LambdaUpdateWrapper<Product> productLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        productLambdaUpdateWrapper.eq(Product::getId, productDto.getId());
        if (productDto.getName() != null){
            productLambdaUpdateWrapper.set(Product::getName, productDto.getName());
        }
        if (productDto.getPoints() != null){
            productLambdaUpdateWrapper.set(Product::getPoints, productDto.getPoints());
        }
        if (productDto.getTime() != null){
            productLambdaUpdateWrapper.set(Product::getTime, productDto.getTime());
        }
        if (productDto.getIsCompany() != null){
            productLambdaUpdateWrapper.set(Product::getIsCompany, productDto.getIsCompany());
        }
        if (productDto.getDesc() != null){
            productLambdaUpdateWrapper.set(Product::getDesc, productDto.getDesc());
        }
        productService.update(productLambdaUpdateWrapper);
        return Result.success("修改成功");
    }

    @Operation(summary = "删除产品分")
    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        productService.removeById(id);
        return Result.success("删除成功");
    }

    @Operation(summary = "查询产品积分")
    @GetMapping("/query/list")
    public Result query(){
        List<ProductDto> productDtos = productService.search();
        return Result.success(productDtos);
    }

    @Operation(summary = "关键词分页查询")
    @PostMapping("/search")
    public Result searcAuther(@RequestBody PageParam pageParam) {
        PageResult<Product> autherList = productService.searchProduct(pageParam);
        return Result.success(autherList);
    }

}
